home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl70b2.lha / tcl7.0b2 / tclBasic.c < prev    next >
C/C++ Source or Header  |  1993-07-07  |  35KB  |  1,296 lines

  1. /* 
  2.  * tclBasic.c --
  3.  *
  4.  *    Contains the basic facilities for TCL command interpretation,
  5.  *    including interpreter creation and deletion, command creation
  6.  *    and deletion, and command parsing and execution.
  7.  *
  8.  * Copyright (c) 1987-1993 The Regents of the University of California.
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and its documentation for any purpose, provided that the
  14.  * above copyright notice and the following two paragraphs appear in
  15.  * all copies of this software.
  16.  * 
  17.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  18.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  19.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  20.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  21.  *
  22.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  23.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  24.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  25.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  26.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  27.  */
  28.  
  29. #ifndef lint
  30. static char rcsid[] = "$Header: /user6/ouster/tcl/RCS/tclBasic.c,v 1.147 93/07/07 16:24:26 ouster Exp $ SPRITE (Berkeley)";
  31. #endif
  32.  
  33. #include "tclInt.h"
  34. #ifndef TCL_GENERIC_ONLY
  35. #   include "tclUnix.h"
  36. #endif
  37.  
  38. /*
  39.  * The following structure defines all of the commands in the Tcl core,
  40.  * and the C procedures that execute them.
  41.  */
  42.  
  43. typedef struct {
  44.     char *name;            /* Name of command. */
  45.     Tcl_CmdProc *proc;        /* Procedure that executes command. */
  46. } CmdInfo;
  47.  
  48. /*
  49.  * Built-in commands, and the procedures associated with them:
  50.  */
  51.  
  52. static CmdInfo builtInCmds[] = {
  53.     /*
  54.      * Commands in the generic core:
  55.      */
  56.  
  57.     {"append",        Tcl_AppendCmd},
  58.     {"array",        Tcl_ArrayCmd},
  59.     {"break",        Tcl_BreakCmd},
  60.     {"case",        Tcl_CaseCmd},
  61.     {"catch",        Tcl_CatchCmd},
  62.     {"concat",        Tcl_ConcatCmd},
  63.     {"continue",    Tcl_ContinueCmd},
  64.     {"error",        Tcl_ErrorCmd},
  65.     {"eval",        Tcl_EvalCmd},
  66.     {"expr",        Tcl_ExprCmd},
  67.     {"for",        Tcl_ForCmd},
  68.     {"foreach",        Tcl_ForeachCmd},
  69.     {"format",        Tcl_FormatCmd},
  70.     {"global",        Tcl_GlobalCmd},
  71.     {"history",        Tcl_HistoryCmd},
  72.     {"if",        Tcl_IfCmd},
  73.     {"incr",        Tcl_IncrCmd},
  74.     {"info",        Tcl_InfoCmd},
  75.     {"join",        Tcl_JoinCmd},
  76.     {"lappend",        Tcl_LappendCmd},
  77.     {"lindex",        Tcl_LindexCmd},
  78.     {"linsert",        Tcl_LinsertCmd},
  79.     {"list",        Tcl_ListCmd},
  80.     {"llength",        Tcl_LlengthCmd},
  81.     {"lrange",        Tcl_LrangeCmd},
  82.     {"lreplace",    Tcl_LreplaceCmd},
  83.     {"lsearch",        Tcl_LsearchCmd},
  84.     {"lsort",        Tcl_LsortCmd},
  85.     {"proc",        Tcl_ProcCmd},
  86.     {"regexp",        Tcl_RegexpCmd},
  87.     {"regsub",        Tcl_RegsubCmd},
  88.     {"rename",        Tcl_RenameCmd},
  89.     {"return",        Tcl_ReturnCmd},
  90.     {"scan",        Tcl_ScanCmd},
  91.     {"set",        Tcl_SetCmd},
  92.     {"split",        Tcl_SplitCmd},
  93.     {"string",        Tcl_StringCmd},
  94.     {"switch",        Tcl_SwitchCmd},
  95.     {"trace",        Tcl_TraceCmd},
  96.     {"unset",        Tcl_UnsetCmd},
  97.     {"uplevel",        Tcl_UplevelCmd},
  98.     {"upvar",        Tcl_UpvarCmd},
  99.     {"while",        Tcl_WhileCmd},
  100.  
  101.     /*
  102.      * Commands in the UNIX core:
  103.      */
  104.  
  105. #ifndef TCL_GENERIC_ONLY
  106.     {"cd",        Tcl_CdCmd},
  107.     {"close",        Tcl_CloseCmd},
  108.     {"eof",        Tcl_EofCmd},
  109.     {"exec",        Tcl_ExecCmd},
  110.     {"exit",        Tcl_ExitCmd},
  111.     {"file",        Tcl_FileCmd},
  112.     {"flush",        Tcl_FlushCmd},
  113.     {"gets",        Tcl_GetsCmd},
  114.     {"glob",        Tcl_GlobCmd},
  115.     {"open",        Tcl_OpenCmd},
  116.     {"pid",        Tcl_PidCmd},
  117.     {"puts",        Tcl_PutsCmd},
  118.     {"pwd",        Tcl_PwdCmd},
  119.     {"read",        Tcl_ReadCmd},
  120.     {"seek",        Tcl_SeekCmd},
  121.     {"source",        Tcl_SourceCmd},
  122.     {"tell",        Tcl_TellCmd},
  123.     {"time",        Tcl_TimeCmd},
  124. #endif /* TCL_GENERIC_ONLY */
  125.     {NULL,        (Tcl_CmdProc *) NULL}
  126. };
  127.  
  128. /*
  129.  *----------------------------------------------------------------------
  130.  *
  131.  * Tcl_CreateInterp --
  132.  *
  133.  *    Create a new TCL command interpreter.
  134.  *
  135.  * Results:
  136.  *    The return value is a token for the interpreter, which may be
  137.  *    used in calls to procedures like Tcl_CreateCmd, Tcl_Eval, or
  138.  *    Tcl_DeleteInterp.
  139.  *
  140.  * Side effects:
  141.  *    The command interpreter is initialized with an empty variable
  142.  *    table and the built-in commands.  SIGPIPE signals are set to
  143.  *    be ignored (see comment below for details).
  144.  *
  145.  *----------------------------------------------------------------------
  146.  */
  147.  
  148. Tcl_Interp *
  149. Tcl_CreateInterp()
  150. {
  151.     register Interp *iPtr;
  152.     register Command *cmdPtr;
  153.     register CmdInfo *cmdInfoPtr;
  154.     int i;
  155.     static int firstInterp = 1;
  156.  
  157.     iPtr = (Interp *) ckalloc(sizeof(Interp));
  158.     iPtr->result = iPtr->resultSpace;
  159.     iPtr->freeProc = 0;
  160.     iPtr->errorLine = 0;
  161.     Tcl_InitHashTable(&iPtr->commandTable, TCL_STRING_KEYS);
  162.     Tcl_InitHashTable(&iPtr->mathFuncTable, TCL_STRING_KEYS);
  163.     Tcl_InitHashTable(&iPtr->globalTable, TCL_STRING_KEYS);
  164.     iPtr->numLevels = 0;
  165.     iPtr->maxNestingDepth = 1000;
  166.     iPtr->framePtr = NULL;
  167.     iPtr->varFramePtr = NULL;
  168.     iPtr->activeTracePtr = NULL;
  169.     iPtr->returnCode = TCL_OK;
  170.     iPtr->numEvents = 0;
  171.     iPtr->events = NULL;
  172.     iPtr->curEvent = 0;
  173.     iPtr->curEventNum = 0;
  174.     iPtr->revPtr = NULL;
  175.     iPtr->historyFirst = NULL;
  176.     iPtr->revDisables = 1;
  177.     iPtr->evalFirst = iPtr->evalLast = NULL;
  178.     iPtr->appendResult = NULL;
  179.     iPtr->appendAvl = 0;
  180.     iPtr->appendUsed = 0;
  181.     iPtr->numFiles = 0;
  182.     iPtr->oFilePtrArray = NULL;
  183.     for (i = 0; i < NUM_REGEXPS; i++) {
  184.     iPtr->patterns[i] = NULL;
  185.     iPtr->patLengths[i] = -1;
  186.     iPtr->regexps[i] = NULL;
  187.     }
  188.     strcpy(iPtr->pdFormat, DEFAULT_PD_FORMAT);
  189.     iPtr->pdPrec = DEFAULT_PD_PREC;
  190.     iPtr->cmdCount = 0;
  191.     iPtr->noEval = 0;
  192.     iPtr->evalFlags = 0;
  193.     iPtr->scriptFile = NULL;
  194.     iPtr->flags = 0;
  195.     iPtr->tracePtr = NULL;
  196.     iPtr->deleteCallbackPtr = NULL;
  197.     iPtr->resultSpace[0] = 0;
  198.  
  199.     /*
  200.      * Create the built-in commands.  Do it here, rather than calling
  201.      * Tcl_CreateCommand, because it's faster (there's no need to
  202.      * check for a pre-existing command by the same name).
  203.      */
  204.  
  205.     for (cmdInfoPtr = builtInCmds; cmdInfoPtr->name != NULL; cmdInfoPtr++) {
  206.     int new;
  207.     Tcl_HashEntry *hPtr;
  208.  
  209.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable,
  210.         cmdInfoPtr->name, &new);
  211.     if (new) {
  212.         cmdPtr = (Command *) ckalloc(sizeof(Command));
  213.         cmdPtr->proc = cmdInfoPtr->proc;
  214.         cmdPtr->clientData = (ClientData) NULL;
  215.         cmdPtr->deleteProc = NULL;
  216.         cmdPtr->deleteData = (ClientData) NULL;
  217.         Tcl_SetHashValue(hPtr, cmdPtr);
  218.     }
  219.     }
  220.  
  221. #ifndef TCL_GENERIC_ONLY
  222.     TclSetupEnv((Tcl_Interp *) iPtr);
  223.  
  224.     /*
  225.      * The code below causes SIGPIPE (broken pipe) errors to
  226.      * be ignored.  This is needed so that Tcl processes don't
  227.      * die if they create child processes (e.g. using "exec" or
  228.      * "open") that terminate prematurely.  The signal handler
  229.      * is only set up when the first interpreter is created; 
  230.      * after this the application can override the handler with
  231.      * a different one of its own, if it wants.
  232.      */
  233.  
  234.     if (firstInterp) {
  235.     (void) signal(SIGPIPE, SIG_IGN);
  236.     firstInterp = 0;
  237.     }
  238. #endif
  239.  
  240.     Tcl_TraceVar2((Tcl_Interp *) iPtr, "tcl_precision", (char *) NULL,
  241.         TCL_GLOBAL_ONLY|TCL_TRACE_WRITES|TCL_TRACE_UNSETS,
  242.         TclPrecTraceProc, (ClientData) NULL);
  243.     return (Tcl_Interp *) iPtr;
  244. }
  245.  
  246. /*
  247.  *--------------------------------------------------------------
  248.  *
  249.  * Tcl_CallWhenDeleted --
  250.  *
  251.  *    Arrange for a procedure to be called before a given
  252.  *    interpreter is deleted.
  253.  *
  254.  * Results:
  255.  *    None.
  256.  *
  257.  * Side effects:
  258.  *    When Tcl_DeleteInterp is invoked to delete interp,
  259.  *    proc will be invoked.  See the manual entry for
  260.  *    details.
  261.  *
  262.  *--------------------------------------------------------------
  263.  */
  264.  
  265. void
  266. Tcl_CallWhenDeleted(interp, proc, clientData)
  267.     Tcl_Interp *interp;        /* Interpreter to watch. */
  268.     Tcl_InterpDeleteProc *proc;    /* Procedure to call when interpreter
  269.                  * is about to be deleted. */
  270.     ClientData clientData;    /* One-word value to pass to proc. */
  271. {
  272.     DeleteCallback *dcPtr, *prevPtr;
  273.     Interp *iPtr = (Interp *) interp;
  274.  
  275.     dcPtr = (DeleteCallback *) ckalloc(sizeof(DeleteCallback));
  276.     dcPtr->proc = proc;
  277.     dcPtr->clientData = clientData;
  278.     dcPtr->nextPtr = NULL;
  279.     if (iPtr->deleteCallbackPtr == NULL) {
  280.     iPtr->deleteCallbackPtr = dcPtr;
  281.     } else {
  282.     prevPtr = iPtr->deleteCallbackPtr;
  283.     while (prevPtr->nextPtr != NULL) {
  284.         prevPtr = prevPtr->nextPtr;
  285.     }
  286.     prevPtr->nextPtr = dcPtr;
  287.     }
  288. }
  289.  
  290. /*
  291.  *----------------------------------------------------------------------
  292.  *
  293.  * Tcl_DeleteInterp --
  294.  *
  295.  *    Delete an interpreter and free up all of the resources associated
  296.  *    with it.
  297.  *
  298.  * Results:
  299.  *    None.
  300.  *
  301.  * Side effects:
  302.  *    The interpreter is destroyed.  The caller should never again
  303.  *    use the interp token.
  304.  *
  305.  *----------------------------------------------------------------------
  306.  */
  307.  
  308. void
  309. Tcl_DeleteInterp(interp)
  310.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  311.                  * by a previous call to Tcl_CreateInterp). */
  312. {
  313.     Interp *iPtr = (Interp *) interp;
  314.     Tcl_HashEntry *hPtr;
  315.     Tcl_HashSearch search;
  316.     register Command *cmdPtr;
  317.     DeleteCallback *dcPtr;
  318.     int i;
  319.  
  320.     /*
  321.      * If the interpreter is in use, delay the deletion until later.
  322.      */
  323.  
  324.     iPtr->flags |= DELETED;
  325.     if (iPtr->numLevels != 0) {
  326.     return;
  327.     }
  328.  
  329.     /*
  330.      * Invoke deletion callbacks.
  331.      */
  332.  
  333.     while (iPtr->deleteCallbackPtr != NULL) {
  334.     dcPtr = iPtr->deleteCallbackPtr;
  335.     iPtr->deleteCallbackPtr = dcPtr->nextPtr;
  336.     (*dcPtr->proc)(dcPtr->clientData, interp);
  337.     ckfree((char *) dcPtr);
  338.     }
  339.  
  340.     /*
  341.      * Free up any remaining resources associated with the
  342.      * interpreter.
  343.      */
  344.  
  345.     for (hPtr = Tcl_FirstHashEntry(&iPtr->commandTable, &search);
  346.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  347.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  348.     if (cmdPtr->deleteProc != NULL) { 
  349.         (*cmdPtr->deleteProc)(cmdPtr->deleteData);
  350.     }
  351.     ckfree((char *) cmdPtr);
  352.     }
  353.     Tcl_DeleteHashTable(&iPtr->commandTable);
  354.     for (hPtr = Tcl_FirstHashEntry(&iPtr->mathFuncTable, &search);
  355.         hPtr != NULL; hPtr = Tcl_NextHashEntry(&search)) {
  356.     ckfree((char *) Tcl_GetHashValue(hPtr));
  357.     }
  358.     Tcl_DeleteHashTable(&iPtr->mathFuncTable);
  359.     TclDeleteVars(iPtr, &iPtr->globalTable);
  360.     if (iPtr->events != NULL) {
  361.     int i;
  362.  
  363.     for (i = 0; i < iPtr->numEvents; i++) {
  364.         ckfree(iPtr->events[i].command);
  365.     }
  366.     ckfree((char *) iPtr->events);
  367.     }
  368.     while (iPtr->revPtr != NULL) {
  369.     HistoryRev *nextPtr = iPtr->revPtr->nextPtr;
  370.  
  371.     ckfree((char *) iPtr->revPtr);
  372.     iPtr->revPtr = nextPtr;
  373.     }
  374.     if (iPtr->appendResult != NULL) {
  375.     ckfree(iPtr->appendResult);
  376.     }
  377. #ifndef TCL_GENERIC_ONLY
  378.     if (iPtr->numFiles > 0) {
  379.     for (i = 0; i < iPtr->numFiles; i++) {
  380.         OpenFile *oFilePtr;
  381.     
  382.         oFilePtr = iPtr->oFilePtrArray[i];
  383.         if (oFilePtr == NULL) {
  384.         continue;
  385.         }
  386.         if (i >= 3) {
  387.         fclose(oFilePtr->f);
  388.         if (oFilePtr->f2 != NULL) {
  389.             fclose(oFilePtr->f2);
  390.         }
  391.         if (oFilePtr->numPids > 0) {
  392.             Tcl_DetachPids(oFilePtr->numPids, oFilePtr->pidPtr);
  393.             ckfree((char *) oFilePtr->pidPtr);
  394.         }
  395.         }
  396.         ckfree((char *) oFilePtr);
  397.     }
  398.     ckfree((char *) iPtr->oFilePtrArray);
  399.     }
  400. #endif
  401.     for (i = 0; i < NUM_REGEXPS; i++) {
  402.     if (iPtr->patterns[i] == NULL) {
  403.         break;
  404.     }
  405.     ckfree(iPtr->patterns[i]);
  406.     ckfree((char *) iPtr->regexps[i]);
  407.     }
  408.     while (iPtr->tracePtr != NULL) {
  409.     Trace *nextPtr = iPtr->tracePtr->nextPtr;
  410.  
  411.     ckfree((char *) iPtr->tracePtr);
  412.     iPtr->tracePtr = nextPtr;
  413.     }
  414.     ckfree((char *) iPtr);
  415. }
  416.  
  417. /*
  418.  *----------------------------------------------------------------------
  419.  *
  420.  * Tcl_CreateCommand --
  421.  *
  422.  *    Define a new command in a command table.
  423.  *
  424.  * Results:
  425.  *    None.
  426.  *
  427.  * Side effects:
  428.  *    If a command named cmdName already exists for interp, it is
  429.  *    deleted.  In the future, when cmdName is seen as the name of
  430.  *    a command by Tcl_Eval, proc will be called.  When the command
  431.  *    is deleted from the table, deleteProc will be called.  See the
  432.  *    manual entry for details on the calling sequence.
  433.  *
  434.  *----------------------------------------------------------------------
  435.  */
  436.  
  437. void
  438. Tcl_CreateCommand(interp, cmdName, proc, clientData, deleteProc)
  439.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  440.                  * by a previous call to Tcl_CreateInterp). */
  441.     char *cmdName;        /* Name of command. */
  442.     Tcl_CmdProc *proc;        /* Command procedure to associate with
  443.                  * cmdName. */
  444.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  445.     Tcl_CmdDeleteProc *deleteProc;
  446.                 /* If not NULL, gives a procedure to call when
  447.                  * this command is deleted. */
  448. {
  449.     Interp *iPtr = (Interp *) interp;
  450.     register Command *cmdPtr;
  451.     Tcl_HashEntry *hPtr;
  452.     int new;
  453.  
  454.     hPtr = Tcl_CreateHashEntry(&iPtr->commandTable, cmdName, &new);
  455.     if (!new) {
  456.     /*
  457.      * Command already exists:  delete the old one.
  458.      */
  459.  
  460.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  461.     if (cmdPtr->deleteProc != NULL) {
  462.         (*cmdPtr->deleteProc)(cmdPtr->deleteData);
  463.     }
  464.     } else {
  465.     cmdPtr = (Command *) ckalloc(sizeof(Command));
  466.     Tcl_SetHashValue(hPtr, cmdPtr);
  467.     }
  468.     cmdPtr->proc = proc;
  469.     cmdPtr->clientData = clientData;
  470.     cmdPtr->deleteProc = deleteProc;
  471.     cmdPtr->deleteData = clientData;
  472. }
  473.  
  474. /*
  475.  *----------------------------------------------------------------------
  476.  *
  477.  * Tcl_SetCommandInfo --
  478.  *
  479.  *    Modifies various information about a Tcl command.
  480.  *
  481.  * Results:
  482.  *    If cmdName exists in interp, then the information at *infoPtr
  483.  *    is stored with the command in place of the current information
  484.  *    and 1 is returned.  If the command doesn't exist then 0 is
  485.  *    returned.
  486.  *
  487.  * Side effects:
  488.  *    None.
  489.  *
  490.  *----------------------------------------------------------------------
  491.  */
  492.  
  493. int
  494. Tcl_SetCommandInfo(interp, cmdName, infoPtr)
  495.     Tcl_Interp *interp;            /* Interpreter in which to look
  496.                      * for command. */
  497.     char *cmdName;            /* Name of desired command. */
  498.     Tcl_CmdInfo *infoPtr;        /* Where to store information about
  499.                      * command. */
  500. {
  501.     Tcl_HashEntry *hPtr;
  502.     Command *cmdPtr;
  503.  
  504.     hPtr = Tcl_FindHashEntry(&((Interp *) interp)->commandTable, cmdName);
  505.     if (hPtr == NULL) {
  506.     return 0;
  507.     }
  508.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  509.     cmdPtr->proc = infoPtr->proc;
  510.     cmdPtr->clientData = infoPtr->clientData;
  511.     cmdPtr->deleteProc = infoPtr->deleteProc;
  512.     cmdPtr->deleteData = infoPtr->deleteData;
  513.     return 1;
  514. }
  515.  
  516. /*
  517.  *----------------------------------------------------------------------
  518.  *
  519.  * Tcl_GetCommandInfo --
  520.  *
  521.  *    Returns various information about a Tcl command.
  522.  *
  523.  * Results:
  524.  *    If cmdName exists in interp, then *infoPtr is modified to
  525.  *    hold information about cmdName and 1 is returned.  If the
  526.  *    command doesn't exist then 0 is returned and *infoPtr isn't
  527.  *    modified.
  528.  *
  529.  * Side effects:
  530.  *    None.
  531.  *
  532.  *----------------------------------------------------------------------
  533.  */
  534.  
  535. int
  536. Tcl_GetCommandInfo(interp, cmdName, infoPtr)
  537.     Tcl_Interp *interp;            /* Interpreter in which to look
  538.                      * for command. */
  539.     char *cmdName;            /* Name of desired command. */
  540.     Tcl_CmdInfo *infoPtr;        /* Where to store information about
  541.                      * command. */
  542. {
  543.     Tcl_HashEntry *hPtr;
  544.     Command *cmdPtr;
  545.  
  546.     hPtr = Tcl_FindHashEntry(&((Interp *) interp)->commandTable, cmdName);
  547.     if (hPtr == NULL) {
  548.     return 0;
  549.     }
  550.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  551.     infoPtr->proc = cmdPtr->proc;
  552.     infoPtr->clientData = cmdPtr->clientData;
  553.     infoPtr->deleteProc = cmdPtr->deleteProc;
  554.     infoPtr->deleteData = cmdPtr->deleteData;
  555.     return 1;
  556. }
  557.  
  558. /*
  559.  *----------------------------------------------------------------------
  560.  *
  561.  * Tcl_DeleteCommand --
  562.  *
  563.  *    Remove the given command from the given interpreter.
  564.  *
  565.  * Results:
  566.  *    0 is returned if the command was deleted successfully.
  567.  *    -1 is returned if there didn't exist a command by that
  568.  *    name.
  569.  *
  570.  * Side effects:
  571.  *    CmdName will no longer be recognized as a valid command for
  572.  *    interp.
  573.  *
  574.  *----------------------------------------------------------------------
  575.  */
  576.  
  577. int
  578. Tcl_DeleteCommand(interp, cmdName)
  579.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  580.                  * by a previous call to Tcl_CreateInterp). */
  581.     char *cmdName;        /* Name of command to remove. */
  582. {
  583.     Interp *iPtr = (Interp *) interp;
  584.     Tcl_HashEntry *hPtr;
  585.     Command *cmdPtr;
  586.  
  587.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, cmdName);
  588.     if (hPtr == NULL) {
  589.     return -1;
  590.     }
  591.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  592.     if (cmdPtr->deleteProc != NULL) {
  593.     (*cmdPtr->deleteProc)(cmdPtr->deleteData);
  594.     }
  595.     ckfree((char *) cmdPtr);
  596.     Tcl_DeleteHashEntry(hPtr);
  597.     return 0;
  598. }
  599.  
  600. /*
  601.  *-----------------------------------------------------------------
  602.  *
  603.  * Tcl_Eval --
  604.  *
  605.  *    Parse and execute a command in the Tcl language.
  606.  *
  607.  * Results:
  608.  *    The return value is one of the return codes defined in tcl.hd
  609.  *    (such as TCL_OK), and interp->result contains a string value
  610.  *    to supplement the return code.  The value of interp->result
  611.  *    will persist only until the next call to Tcl_Eval:  copy it or
  612.  *    lose it! *TermPtr is filled in with the character just after
  613.  *    the last one that was part of the command (usually a NULL
  614.  *    character or a closing bracket).
  615.  *
  616.  * Side effects:
  617.  *    Almost certainly;  depends on the command.
  618.  *
  619.  *-----------------------------------------------------------------
  620.  */
  621.  
  622. int
  623. Tcl_Eval(interp, cmd)
  624.     Tcl_Interp *interp;        /* Token for command interpreter (returned
  625.                  * by a previous call to Tcl_CreateInterp). */
  626.     char *cmd;            /* Pointer to TCL command to interpret. */
  627. {
  628.     /*
  629.      * The storage immediately below is used to generate a copy
  630.      * of the command, after all argument substitutions.  Pv will
  631.      * contain the argv values passed to the command procedure.
  632.      */
  633.  
  634. #   define NUM_CHARS 200
  635.     char copyStorage[NUM_CHARS];
  636.     ParseValue pv;
  637.     char *oldBuffer;
  638.  
  639.     /*
  640.      * This procedure generates an (argv, argc) array for the command,
  641.      * It starts out with stack-allocated space but uses dynamically-
  642.      * allocated storage to increase it if needed.
  643.      */
  644.  
  645. #   define NUM_ARGS 10
  646.     char *(argStorage[NUM_ARGS]);
  647.     char **argv = argStorage;
  648.     int argc;
  649.     int argSize = NUM_ARGS;
  650.  
  651.     register char *src;            /* Points to current character
  652.                      * in cmd. */
  653.     char termChar;            /* Return when this character is found
  654.                      * (either ']' or '\0').  Zero means
  655.                      * that newlines terminate commands. */
  656.     int flags;                /* Interp->evalFlags value when the
  657.                      * procedure was called. */
  658.     int result;                /* Return value. */
  659.     register Interp *iPtr = (Interp *) interp;
  660.     Tcl_HashEntry *hPtr;
  661.     Command *cmdPtr;
  662.     char *termPtr;            /* Contains character just after the
  663.                      * last one in the command. */
  664.     char *cmdStart;            /* Points to first non-blank char. in
  665.                      * command (used in calling trace
  666.                      * procedures). */
  667.     char *ellipsis = "";        /* Used in setting errorInfo variable;
  668.                      * set to "..." to indicate that not
  669.                      * all of offending command is included
  670.                      * in errorInfo.  "" means that the
  671.                      * command is all there. */
  672.     register Trace *tracePtr;
  673.  
  674.     /*
  675.      * Initialize the result to an empty string and clear out any
  676.      * error information.  This makes sure that we return an empty
  677.      * result if there are no commands in the command string.
  678.      */
  679.  
  680.     Tcl_FreeResult((Tcl_Interp *) iPtr);
  681.     iPtr->result = iPtr->resultSpace;
  682.     iPtr->resultSpace[0] = 0;
  683.     result = TCL_OK;
  684.  
  685.     /*
  686.      * Initialize the area in which command copies will be assembled.
  687.      */
  688.  
  689.     pv.buffer = copyStorage;
  690.     pv.end = copyStorage + NUM_CHARS - 1;
  691.     pv.expandProc = TclExpandParseValue;
  692.     pv.clientData = (ClientData) NULL;
  693.  
  694.     src = cmd;
  695.     flags = iPtr->evalFlags;
  696.     iPtr->evalFlags = 0;
  697.     if (flags & TCL_BRACKET_TERM) {
  698.     termChar = ']';
  699.     } else {
  700.     termChar = 0;
  701.     }
  702.     termPtr = src;
  703.     cmdStart = src;
  704.  
  705.     /*
  706.      * Check depth of nested calls to Tcl_Eval:  if this gets too large,
  707.      * it's probably because of an infinite loop somewhere.
  708.      */
  709.  
  710.     iPtr->numLevels++;
  711.     if (iPtr->numLevels > iPtr->maxNestingDepth) {
  712.     iPtr->numLevels--;
  713.     iPtr->result =  "too many nested calls to Tcl_Eval (infinite loop?)";
  714.     iPtr->termPtr = termPtr;
  715.     return TCL_ERROR;
  716.     }
  717.  
  718.     /*
  719.      * There can be many sub-commands (separated by semi-colons or
  720.      * newlines) in one command string.  This outer loop iterates over
  721.      * individual commands.
  722.      */
  723.  
  724.     while (*src != termChar) {
  725.     iPtr->flags &= ~(ERR_IN_PROGRESS | ERROR_CODE_SET);
  726.  
  727.     /*
  728.      * Skim off leading white space and semi-colons, and skip
  729.      * comments.
  730.      */
  731.  
  732.     while (1) {
  733.         register char c = *src;
  734.  
  735.         if ((CHAR_TYPE(c) != TCL_SPACE) && (c != ';') && (c != '\n')) {
  736.         break;
  737.         }
  738.         src += 1;
  739.     }
  740.     if (*src == '#') {
  741.         for (src++; *src != 0; src++) {
  742.         if ((*src == '\n') && (src[-1] != '\\')) {
  743.             src++;
  744.             break;
  745.         }
  746.         }
  747.         continue;
  748.     }
  749.     cmdStart = src;
  750.  
  751.     /*
  752.      * Parse the words of the command, generating the argc and
  753.      * argv for the command procedure.  May have to call
  754.      * TclParseWords several times, expanding the argv array
  755.      * between calls.
  756.      */
  757.  
  758.     pv.next = oldBuffer = pv.buffer;
  759.     argc = 0;
  760.     while (1) {
  761.         int newArgs, maxArgs;
  762.         char **newArgv;
  763.         int i;
  764.  
  765.         /*
  766.          * Note:  the "- 2" below guarantees that we won't use the
  767.          * last two argv slots here.  One is for a NULL pointer to
  768.          * mark the end of the list, and the other is to leave room
  769.          * for inserting the command name "unknown" as the first
  770.          * argument (see below).
  771.          */
  772.  
  773.         maxArgs = argSize - argc - 2;
  774.         result = TclParseWords((Tcl_Interp *) iPtr, src, flags,
  775.             maxArgs, &termPtr, &newArgs, &argv[argc], &pv);
  776.         src = termPtr;
  777.         if (result != TCL_OK) {
  778.         ellipsis = "...";
  779.         goto done;
  780.         }
  781.  
  782.         /*
  783.          * Careful!  Buffer space may have gotten reallocated while
  784.          * parsing words.  If this happened, be sure to update all
  785.          * of the older argv pointers to refer to the new space.
  786.          */
  787.  
  788.         if (oldBuffer != pv.buffer) {
  789.         int i;
  790.  
  791.         for (i = 0; i < argc; i++) {
  792.             argv[i] = pv.buffer + (argv[i] - oldBuffer);
  793.         }
  794.         oldBuffer = pv.buffer;
  795.         }
  796.         argc += newArgs;
  797.         if (newArgs < maxArgs) {
  798.         argv[argc] = (char *) NULL;
  799.         break;
  800.         }
  801.  
  802.         /*
  803.          * Args didn't all fit in the current array.  Make it bigger.
  804.          */
  805.  
  806.         argSize *= 2;
  807.         newArgv = (char **)
  808.             ckalloc((unsigned) argSize * sizeof(char *));
  809.         for (i = 0; i < argc; i++) {
  810.         newArgv[i] = argv[i];
  811.         }
  812.         if (argv != argStorage) {
  813.         ckfree((char *) argv);
  814.         }
  815.         argv = newArgv;
  816.     }
  817.  
  818.     /*
  819.      * If this is an empty command (or if we're just parsing
  820.      * commands without evaluating them), then just skip to the
  821.      * next command.
  822.      */
  823.  
  824.     if ((argc == 0) || iPtr->noEval) {
  825.         continue;
  826.     }
  827.     argv[argc] = NULL;
  828.  
  829.     /*
  830.      * Save information for the history module, if needed.
  831.      */
  832.  
  833.     if (flags & TCL_RECORD_BOUNDS) {
  834.         iPtr->evalFirst = cmdStart;
  835.         iPtr->evalLast = src-1;
  836.     }
  837.  
  838.     /*
  839.      * Find the procedure to execute this command.  If there isn't
  840.      * one, then see if there is a command "unknown".  If so,
  841.      * invoke it instead, passing it the words of the original
  842.      * command as arguments.
  843.      */
  844.  
  845.     hPtr = Tcl_FindHashEntry(&iPtr->commandTable, argv[0]);
  846.     if (hPtr == NULL) {
  847.         int i;
  848.  
  849.         hPtr = Tcl_FindHashEntry(&iPtr->commandTable, "unknown");
  850.         if (hPtr == NULL) {
  851.         Tcl_ResetResult(interp);
  852.         Tcl_AppendResult(interp, "invalid command name: \"",
  853.             argv[0], "\"", (char *) NULL);
  854.         result = TCL_ERROR;
  855.         goto done;
  856.         }
  857.         for (i = argc; i >= 0; i--) {
  858.         argv[i+1] = argv[i];
  859.         }
  860.         argv[0] = "unknown";
  861.         argc++;
  862.     }
  863.     cmdPtr = (Command *) Tcl_GetHashValue(hPtr);
  864.  
  865.     /*
  866.      * Call trace procedures, if any.
  867.      */
  868.  
  869.     for (tracePtr = iPtr->tracePtr; tracePtr != NULL;
  870.         tracePtr = tracePtr->nextPtr) {
  871.         char saved;
  872.  
  873.         if (tracePtr->level < iPtr->numLevels) {
  874.         continue;
  875.         }
  876.         saved = *src;
  877.         *src = 0;
  878.         (*tracePtr->proc)(tracePtr->clientData, interp, iPtr->numLevels,
  879.             cmdStart, cmdPtr->proc, cmdPtr->clientData, argc, argv);
  880.         *src = saved;
  881.     }
  882.  
  883.     /*
  884.      * At long last, invoke the command procedure.  Reset the
  885.      * result to its default empty value first (it could have
  886.      * gotten changed by earlier commands in the same command
  887.      * string).
  888.      */
  889.  
  890.     iPtr->cmdCount++;
  891.     Tcl_FreeResult(iPtr);
  892.     iPtr->result = iPtr->resultSpace;
  893.     iPtr->resultSpace[0] = 0;
  894.     result = (*cmdPtr->proc)(cmdPtr->clientData, interp, argc, argv);
  895.     if (result != TCL_OK) {
  896.         break;
  897.     }
  898.     }
  899.  
  900.     /*
  901.      * Free up any extra resources that were allocated.
  902.      */
  903.  
  904.     done:
  905.     if (pv.buffer != copyStorage) {
  906.     ckfree((char *) pv.buffer);
  907.     }
  908.     if (argv != argStorage) {
  909.     ckfree((char *) argv);
  910.     }
  911.     iPtr->numLevels--;
  912.     if (iPtr->numLevels == 0) {
  913.     if (result == TCL_RETURN) {
  914.         result = TCL_OK;
  915.     }
  916.     if ((result != TCL_OK) && (result != TCL_ERROR)) {
  917.         Tcl_ResetResult(interp);
  918.         if (result == TCL_BREAK) {
  919.         iPtr->result = "invoked \"break\" outside of a loop";
  920.         } else if (result == TCL_CONTINUE) {
  921.         iPtr->result = "invoked \"continue\" outside of a loop";
  922.         } else {
  923.         iPtr->result = iPtr->resultSpace;
  924.         sprintf(iPtr->resultSpace, "command returned bad code: %d",
  925.             result);
  926.         }
  927.         result = TCL_ERROR;
  928.     }
  929.     if (iPtr->flags & DELETED) {
  930.         Tcl_DeleteInterp(interp);
  931.     }
  932.     }
  933.  
  934.     /*
  935.      * If an error occurred, record information about what was being
  936.      * executed when the error occurred.
  937.      */
  938.  
  939.     if ((result == TCL_ERROR) && !(iPtr->flags & ERR_ALREADY_LOGGED)) {
  940.     int numChars;
  941.     register char *p;
  942.  
  943.     /*
  944.      * Compute the line number where the error occurred.
  945.      */
  946.  
  947.     iPtr->errorLine = 1;
  948.     for (p = cmd; p != cmdStart; p++) {
  949.         if (*p == '\n') {
  950.         iPtr->errorLine++;
  951.         }
  952.     }
  953.     for ( ; isspace(*p) || (*p == ';'); p++) {
  954.         if (*p == '\n') {
  955.         iPtr->errorLine++;
  956.         }
  957.     }
  958.  
  959.     /*
  960.      * Figure out how much of the command to print in the error
  961.      * message (up to a certain number of characters, or up to
  962.      * the first new-line).
  963.      */
  964.  
  965.     numChars = src - cmdStart;
  966.     if (numChars > (NUM_CHARS-50)) {
  967.         numChars = NUM_CHARS-50;
  968.         ellipsis = " ...";
  969.     }
  970.  
  971.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  972.         sprintf(copyStorage, "\n    while executing\n\"%.*s%s\"",
  973.             numChars, cmdStart, ellipsis);
  974.     } else {
  975.         sprintf(copyStorage, "\n    invoked from within\n\"%.*s%s\"",
  976.             numChars, cmdStart, ellipsis);
  977.     }
  978.     Tcl_AddErrorInfo(interp, copyStorage);
  979.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  980.     } else {
  981.     iPtr->flags &= ~ERR_ALREADY_LOGGED;
  982.     }
  983.     iPtr->termPtr = termPtr;
  984.     return result;
  985. }
  986.  
  987. /*
  988.  *----------------------------------------------------------------------
  989.  *
  990.  * Tcl_CreateTrace --
  991.  *
  992.  *    Arrange for a procedure to be called to trace command execution.
  993.  *
  994.  * Results:
  995.  *    The return value is a token for the trace, which may be passed
  996.  *    to Tcl_DeleteTrace to eliminate the trace.
  997.  *
  998.  * Side effects:
  999.  *    From now on, proc will be called just before a command procedure
  1000.  *    is called to execute a Tcl command.  Calls to proc will have the
  1001.  *    following form:
  1002.  *
  1003.  *    void
  1004.  *    proc(clientData, interp, level, command, cmdProc, cmdClientData,
  1005.  *        argc, argv)
  1006.  *        ClientData clientData;
  1007.  *        Tcl_Interp *interp;
  1008.  *        int level;
  1009.  *        char *command;
  1010.  *        int (*cmdProc)();
  1011.  *        ClientData cmdClientData;
  1012.  *        int argc;
  1013.  *        char **argv;
  1014.  *    {
  1015.  *    }
  1016.  *
  1017.  *    The clientData and interp arguments to proc will be the same
  1018.  *    as the corresponding arguments to this procedure.  Level gives
  1019.  *    the nesting level of command interpretation for this interpreter
  1020.  *    (0 corresponds to top level).  Command gives the ASCII text of
  1021.  *    the raw command, cmdProc and cmdClientData give the procedure that
  1022.  *    will be called to process the command and the ClientData value it
  1023.  *    will receive, and argc and argv give the arguments to the
  1024.  *    command, after any argument parsing and substitution.  Proc
  1025.  *    does not return a value.
  1026.  *
  1027.  *----------------------------------------------------------------------
  1028.  */
  1029.  
  1030. Tcl_Trace
  1031. Tcl_CreateTrace(interp, level, proc, clientData)
  1032.     Tcl_Interp *interp;        /* Interpreter in which to create the trace. */
  1033.     int level;            /* Only call proc for commands at nesting level
  1034.                  * <= level (1 => top level). */
  1035.     Tcl_CmdTraceProc *proc;    /* Procedure to call before executing each
  1036.                  * command. */
  1037.     ClientData clientData;    /* Arbitrary one-word value to pass to proc. */
  1038. {
  1039.     register Trace *tracePtr;
  1040.     register Interp *iPtr = (Interp *) interp;
  1041.  
  1042.     tracePtr = (Trace *) ckalloc(sizeof(Trace));
  1043.     tracePtr->level = level;
  1044.     tracePtr->proc = proc;
  1045.     tracePtr->clientData = clientData;
  1046.     tracePtr->nextPtr = iPtr->tracePtr;
  1047.     iPtr->tracePtr = tracePtr;
  1048.  
  1049.     return (Tcl_Trace) tracePtr;
  1050. }
  1051.  
  1052. /*
  1053.  *----------------------------------------------------------------------
  1054.  *
  1055.  * Tcl_DeleteTrace --
  1056.  *
  1057.  *    Remove a trace.
  1058.  *
  1059.  * Results:
  1060.  *    None.
  1061.  *
  1062.  * Side effects:
  1063.  *    From now on there will be no more calls to the procedure given
  1064.  *    in trace.
  1065.  *
  1066.  *----------------------------------------------------------------------
  1067.  */
  1068.  
  1069. void
  1070. Tcl_DeleteTrace(interp, trace)
  1071.     Tcl_Interp *interp;        /* Interpreter that contains trace. */
  1072.     Tcl_Trace trace;        /* Token for trace (returned previously by
  1073.                  * Tcl_CreateTrace). */
  1074. {
  1075.     register Interp *iPtr = (Interp *) interp;
  1076.     register Trace *tracePtr = (Trace *) trace;
  1077.     register Trace *tracePtr2;
  1078.  
  1079.     if (iPtr->tracePtr == tracePtr) {
  1080.     iPtr->tracePtr = tracePtr->nextPtr;
  1081.     ckfree((char *) tracePtr);
  1082.     } else {
  1083.     for (tracePtr2 = iPtr->tracePtr; tracePtr2 != NULL;
  1084.         tracePtr2 = tracePtr2->nextPtr) {
  1085.         if (tracePtr2->nextPtr == tracePtr) {
  1086.         tracePtr2->nextPtr = tracePtr->nextPtr;
  1087.         ckfree((char *) tracePtr);
  1088.         return;
  1089.         }
  1090.     }
  1091.     }
  1092. }
  1093.  
  1094. /*
  1095.  *----------------------------------------------------------------------
  1096.  *
  1097.  * Tcl_AddErrorInfo --
  1098.  *
  1099.  *    Add information to a message being accumulated that describes
  1100.  *    the current error.
  1101.  *
  1102.  * Results:
  1103.  *    None.
  1104.  *
  1105.  * Side effects:
  1106.  *    The contents of message are added to the "errorInfo" variable.
  1107.  *    If Tcl_Eval has been called since the current value of errorInfo
  1108.  *    was set, errorInfo is cleared before adding the new message.
  1109.  *
  1110.  *----------------------------------------------------------------------
  1111.  */
  1112.  
  1113. void
  1114. Tcl_AddErrorInfo(interp, message)
  1115.     Tcl_Interp *interp;        /* Interpreter to which error information
  1116.                  * pertains. */
  1117.     char *message;        /* Message to record. */
  1118. {
  1119.     register Interp *iPtr = (Interp *) interp;
  1120.  
  1121.     /*
  1122.      * If an error is already being logged, then the new errorInfo
  1123.      * is the concatenation of the old info and the new message.
  1124.      * If this is the first piece of info for the error, then the
  1125.      * new errorInfo is the concatenation of the message in
  1126.      * interp->result and the new message.
  1127.      */
  1128.  
  1129.     if (!(iPtr->flags & ERR_IN_PROGRESS)) {
  1130.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, interp->result,
  1131.         TCL_GLOBAL_ONLY);
  1132.     iPtr->flags |= ERR_IN_PROGRESS;
  1133.  
  1134.     /*
  1135.      * If the errorCode variable wasn't set by the code that generated
  1136.      * the error, set it to "NONE".
  1137.      */
  1138.  
  1139.     if (!(iPtr->flags & ERROR_CODE_SET)) {
  1140.         (void) Tcl_SetVar2(interp, "errorCode", (char *) NULL, "NONE",
  1141.             TCL_GLOBAL_ONLY);
  1142.     }
  1143.     }
  1144.     Tcl_SetVar2(interp, "errorInfo", (char *) NULL, message,
  1145.         TCL_GLOBAL_ONLY|TCL_APPEND_VALUE);
  1146. }
  1147.  
  1148. /*
  1149.  *----------------------------------------------------------------------
  1150.  *
  1151.  * Tcl_VarEval --
  1152.  *
  1153.  *    Given a variable number of string arguments, concatenate them
  1154.  *    all together and execute the result as a Tcl command.
  1155.  *
  1156.  * Results:
  1157.  *    A standard Tcl return result.  An error message or other
  1158.  *    result may be left in interp->result.
  1159.  *
  1160.  * Side effects:
  1161.  *    Depends on what was done by the command.
  1162.  *
  1163.  *----------------------------------------------------------------------
  1164.  */
  1165.     /* VARARGS2 */ /* ARGSUSED */
  1166. int
  1167. #ifndef lint
  1168. Tcl_VarEval(va_alist)
  1169. #else
  1170. Tcl_VarEval(iPtr, p, va_alist)
  1171.     Tcl_Interp *iPtr;        /* Interpreter in which to execute command. */
  1172.     char *p;            /* One or more strings to concatenate,
  1173.                  * terminated with a NULL string. */
  1174. #endif
  1175.     va_dcl
  1176. {
  1177.     va_list argList;
  1178. #define FIXED_SIZE 200
  1179.     char fixedSpace[FIXED_SIZE+1];
  1180.     int spaceAvl, spaceUsed, length;
  1181.     char *string, *cmd;
  1182.     Tcl_Interp *interp;
  1183.     int result;
  1184.  
  1185.     /*
  1186.      * Copy the strings one after the other into a single larger
  1187.      * string.  Use stack-allocated space for small commands, but if
  1188.      * the command gets too large than call ckalloc to create the
  1189.      * space.
  1190.      */
  1191.  
  1192.     va_start(argList);
  1193.     interp = va_arg(argList, Tcl_Interp *);
  1194.     spaceAvl = FIXED_SIZE;
  1195.     spaceUsed = 0;
  1196.     cmd = fixedSpace;
  1197.     while (1) {
  1198.     string = va_arg(argList, char *);
  1199.     if (string == NULL) {
  1200.         break;
  1201.     }
  1202.     length = strlen(string);
  1203.     if ((spaceUsed + length) > spaceAvl) {
  1204.         char *new;
  1205.  
  1206.         spaceAvl = spaceUsed + length;
  1207.         spaceAvl += spaceAvl/2;
  1208.         new = ckalloc((unsigned) spaceAvl);
  1209.         memcpy((VOID *) new, (VOID *) cmd, spaceUsed);
  1210.         if (cmd != fixedSpace) {
  1211.         ckfree(cmd);
  1212.         }
  1213.         cmd = new;
  1214.     }
  1215.     strcpy(cmd + spaceUsed, string);
  1216.     spaceUsed += length;
  1217.     }
  1218.     va_end(argList);
  1219.     cmd[spaceUsed] = '\0';
  1220.  
  1221.     result = Tcl_Eval(interp, cmd);
  1222.     if (cmd != fixedSpace) {
  1223.     ckfree(cmd);
  1224.     }
  1225.     return result;
  1226. }
  1227.  
  1228. /*
  1229.  *----------------------------------------------------------------------
  1230.  *
  1231.  * Tcl_GlobalEval --
  1232.  *
  1233.  *    Evaluate a command at global level in an interpreter.
  1234.  *
  1235.  * Results:
  1236.  *    A standard Tcl result is returned, and interp->result is
  1237.  *    modified accordingly.
  1238.  *
  1239.  * Side effects:
  1240.  *    The command string is executed in interp, and the execution
  1241.  *    is carried out in the variable context of global level (no
  1242.  *    procedures active), just as if an "uplevel #0" command were
  1243.  *    being executed.
  1244.  *
  1245.  *----------------------------------------------------------------------
  1246.  */
  1247.  
  1248. int
  1249. Tcl_GlobalEval(interp, command)
  1250.     Tcl_Interp *interp;        /* Interpreter in which to evaluate command. */
  1251.     char *command;        /* Command to evaluate. */
  1252. {
  1253.     register Interp *iPtr = (Interp *) interp;
  1254.     int result;
  1255.     CallFrame *savedVarFramePtr;
  1256.  
  1257.     savedVarFramePtr = iPtr->varFramePtr;
  1258.     iPtr->varFramePtr = NULL;
  1259.     result = Tcl_Eval(interp, command);
  1260.     iPtr->varFramePtr = savedVarFramePtr;
  1261.     return result;
  1262. }
  1263.  
  1264. /*
  1265.  *----------------------------------------------------------------------
  1266.  *
  1267.  * Tcl_SetRecursionLimit --
  1268.  *
  1269.  *    Set the maximum number of recursive calls that may be active
  1270.  *    for an interpreter at once.
  1271.  *
  1272.  * Results:
  1273.  *    The return value is the old limit on nesting for interp.
  1274.  *
  1275.  * Side effects:
  1276.  *    None.
  1277.  *
  1278.  *----------------------------------------------------------------------
  1279.  */
  1280.  
  1281. int
  1282. Tcl_SetRecursionLimit(interp, depth)
  1283.     Tcl_Interp *interp;            /* Interpreter whose nesting limit
  1284.                      * is to be set. */
  1285.     int depth;                /* New value for maximimum depth. */
  1286. {
  1287.     Interp *iPtr = (Interp *) interp;
  1288.     int old;
  1289.  
  1290.     old = iPtr->maxNestingDepth;
  1291.     if (depth > 0) {
  1292.     iPtr->maxNestingDepth = depth;
  1293.     }
  1294.     return old;
  1295. }
  1296.